package com.baizhi.wd.service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import com.baizhi.wd.dao.PersonDao;
import com.baizhi.wd.dao.PersonDaoImpl;
import com.baizhi.wd.entity.Person;
import com.baizhi.wd.util.JdbcUtil;

public class TelbookServiceImpl implements TelbookService{
	private PersonDao pd = new PersonDaoImpl();
	//1 �����ϵ����Ҫʹ�õ�ҵ�񷽷�
	public void regist(Person person) {
		Connection con = null;
		try {
			// 1���ù����� �������
			con = JdbcUtil.getCon();
			// 2�����ֶ��ύ
			con.setAutoCommit(false);
			// 3.����dao
			pd = new PersonDaoImpl();
			pd.insertPerson(person);
			// 4�ύ����ÿһ�����ɹ���
			con.commit();
		} catch (Exception e) {
			// 4�ع�����
			try {
				con.rollback();
			} catch (Exception e1) {
				throw new RuntimeException(e1);
			}
			throw new RuntimeException(e);
		} finally {
			// �ͷ���Դ�����ù�����
			JdbcUtil.release(null, null, con);
		}
	}

	//2 !!!!�޸���ϵ��ʱ��Ҫ���õ�ҵ�񷽷�
	public void changePersonMessage(Person person){
		Connection con =null;
  		try{
  		//1���ù����࣡��������
  		con = JdbcUtil.getCon();
  		//�����ֶ�
  		con.setAutoCommit(false);
  		//3����dao
  		pd = new PersonDaoImpl();
  		pd.update(person);
  		//4�ύ����
  		con.commit();
  		}catch(Exception e){
  		//4�ع�����
  			try {con.rollback();} catch (Exception e1) {throw new RuntimeException(e1);}
  			throw new RuntimeException(e);
  			}
  			finally{
  			//5�ͷ���Դ
  			JdbcUtil.release(null, null, con);
  		}
  	}

	//3 ͨ��Id������ϵ��
	public Person getOnePerson(Integer id) {
		Connection con = null;
		try {
			con = JdbcUtil.getCon();
			con.setAutoCommit(false);
			// 3����dao
			pd = new PersonDaoImpl();
			Person per = pd.selectOnePerson(id);

			// 4�ύ����
			con.commit();
			return per;

		} catch (Exception e) {
			//�ع�����
			try {
				con.rollback();
			} catch (SQLException e1) {
				throw new RuntimeException(e1);
			}
			throw new RuntimeException(e);
		} finally {
			// 5�ͷ���Դ

			JdbcUtil.release(null, null, con);
		}
	}

	// 4������е���ϵ�˵�ҵ�񷽷�
	public List<Person> getAllPersons() {
		Connection con = null;
		try {
			con = JdbcUtil.getCon();
			con.setAutoCommit(false);
			// 3����dao
			pd = new PersonDaoImpl();
			List<Person> selectAll = pd.selectAll();
			// 4�ύ����
			con.commit();
			return selectAll;
		} catch (Exception e) {
			//4�ع�����
			try {
				con.rollback();
			} catch (SQLException e1) {
				throw new RuntimeException(e1);
			}
			throw new RuntimeException(e);
		} finally {
			// 5�ͷ���Դ

			JdbcUtil.release(null, null, con);
		}
	}

	//5 �����ϵ�����������ϵ�˵�ҵ�񷽷�
	public List<Person> getPersonByName(String name) {
		Connection con = null;
		try {
			con = JdbcUtil.getCon();
			con.setAutoCommit(false);
			// 3����dao
			pd = new PersonDaoImpl();
			List<Person> selectNameLike = pd.selectNameLike(name);
			// 4�ύ����
			con.commit();
			return selectNameLike;
		} catch (Exception e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				throw new RuntimeException(e1);
			}
			throw new RuntimeException(e);
		} finally {
			// 5�ͷ���Դ

			JdbcUtil.release(null, null, con);
		}
	}

	//6 ����ֻ�����ѯ��ص���ϵ�˵�ҵ�񷽷�
	public List<Person> getPersonByMobile(String mobile) {
		Connection con = null;
		try {
			con = JdbcUtil.getCon();
			con.setAutoCommit(false);
			// 3����dao
			pd = new PersonDaoImpl();
			List<Person> selectMobileLike = pd.selectMobileLike(mobile);
			// 4�ύ����
			con.commit();
			return selectMobileLike;
		} catch (Exception e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				throw new RuntimeException(e1);
			}
			throw new RuntimeException(e);
		} finally {
			// 5�ͷ���Դ
			try {
				con.rollback();
			} catch (SQLException e1) {
				throw new RuntimeException(e1);
			}
			JdbcUtil.release(null, null, con);
		}
	}

	//7 ɾ����ϵ��ʱ��Ҫ���õ�ҵ�񷽷�
	public void dropPerson(Integer id) {
		Connection con = null;
		try {
			con = JdbcUtil.getCon();
			con.setAutoCommit(false);
			// 3����dao
			pd = new PersonDaoImpl();
			pd.deletePerson(id);
			// 4�ύ����
			con.commit();
		} catch (Exception e) {
			// 4.�ع�����
			try {
				con.rollback();
			} catch (SQLException e1) {
			throw new RuntimeException(e1);
			}
			throw new RuntimeException(e);
		} finally {
			
			// �ͷ���Դ
			JdbcUtil.release(null, null, con);
		}
	}
}
